from rest_framework.generics import ListCreateAPIView
from rest_framework.permissions import IsAdminUser
from apps.meiduo_admin.serializers.users import UserSerializer
from apps.meiduo_admin.utils.pagination import MeiduoPagination
from apps.users.models import User
from apps.meiduo_admin.serializers.users import UserCreateSerializer


class UserView(ListCreateAPIView):
    # permission_classes = [IsAdminUser]
    # 查询多条
    # 创建
    # queryset = User.objects.filter(is_staff=False)
    def get_queryset(self):
        # self ===>UserView的对象
        # 获取搜索关键字
        keyword = self.request.query_params.get('keyword')
        # 在用户名上进行模糊查询
        return User.objects.filter(is_staff=False, username__contains=keyword)

    def get_serializer_class(self):
        if self.request.method == 'GET':
            return UserSerializer
        else:
            return UserCreateSerializer

    # 分页
    pagination_class = MeiduoPagination
